package org.itboys.mobile.service.mysql.mobile.product;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;

import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.product.SundayProductCategoryDto;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mysql.service.BaseMySqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_分类_service_mobile
 */
@Service
public class SundayMobileProductCategoryService extends BaseMySqlService<SundayProductCategory> {
    @Autowired
    private ResourceHolder resourceHolder;
    /**
     *
     * 获取商品类目的类目。树结构类型。
     */
    public List<SundayProductCategoryDto> getTree(Long memberId,Long categoryId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("sort","sequence");
        param.put("order","asc");
        List<SundayProductCategoryDto> alls= packageMuilt(super.select(param));
        List<SundayProductCategoryDto> parents = new ArrayList<SundayProductCategoryDto>();
        List<SundayProductCategoryDto> children = new ArrayList<SundayProductCategoryDto>();

        for(SundayProductCategoryDto all:alls){
            if(StringUtils.isNotEmpty(all.getImage())){
                all.setImage(resourceHolder.getStringValue("imgRoot")+all.getImage());
            }else{
                all.setImage(null);
            }
            if(all.getParentId().longValue()==0l){

                //勾选一级类目
                if(LongUtil.isNotZreo(categoryId)&&
                        all.getId()==categoryId.longValue()){
                    all.setIsCheck(SundayCommonConstants.YES);
                }
                parents.add(all);
            }else{
                children.add(all);
            }
        }
        //组装TREE
        for(SundayProductCategoryDto parent:parents){
            List<SundayProductCategoryDto> child = new ArrayList<SundayProductCategoryDto>();
            for(SundayProductCategoryDto c:children){
                if(LongUtil.isNotZreo(c.getParentId())&&
                        c.getParentId().longValue()==parent.getId()){
                    child.add(c);
                }
            }
            parent.setChildren(child);
        }
        return parents;
    }
    public List<SundayProductCategoryDto> getParentCategory(){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("parentId",0l);
        param.put("sort_name","sequence");
        param.put("sort_order","ASC");
        return packageMuilt(super.select(param));
    }
    public SundayProductCategoryDto packageSingle(SundayProductCategory productCategory){
        return new SundayProductCategoryDto(productCategory.getId(), productCategory.getCreateTime(), productCategory.getCreateDate(),
                productCategory.getImage(), productCategory.getName(),productCategory.getParentId(), 0);
    }
    public List<SundayProductCategoryDto> packageMuilt(List<SundayProductCategory> productCategories){
        List<SundayProductCategoryDto> dtos = new ArrayList<SundayProductCategoryDto>();
        for(SundayProductCategory productCategory:productCategories){
            dtos.add(packageSingle(productCategory));
        }
        return  dtos;

    }
}
